/**
 * Aegis 上报使用示例
 * 
 * 这个文件展示了如何在应用中使用 Aegis 进行各种类型的数据上报
 */

import {
  reportEvent,
  info,
  error,
  report,
  reportTime,
  setUser
} from './aegis';

/**
 * 用户行为上报示例
 */
export class AegisReporter {
  
  /**
   * 上报用户登录事件
   */
  static reportUserLogin(userId: string): void {
    // 设置用户ID
    setUser(userId);

    // 上报登录事件
    reportEvent('user_login');

    // 记录登录信息
    info('User login successful', {
      userId,
      timestamp: Date.now()
    });
  }

  /**
   * 上报页面访问
   */
  static reportPageView(pageName: string, route: string): void {
    reportEvent('page_view');

    info('Page view', {
      pageName,
      route,
      timestamp: Date.now()
    });
  }

  /**
   * 上报功能使用
   */
  static reportFeatureUsage(featureName: string, action: string, extra?: Record<string, any>): void {
    reportEvent(`feature_${featureName}_${action}`);

    info(`Feature usage: ${featureName} - ${action}`, {
      featureName,
      action,
      ...extra
    });
  }

  /**
   * 上报AI对话相关事件
   */
  static reportAIChat(action: 'start' | 'send' | 'receive' | 'end', messageCount?: number): void {
    reportEvent(`ai_chat_${action}`);

    info(`AI Chat ${action}`, {
      action,
      messageCount: messageCount || 0,
      timestamp: Date.now()
    });
  }

  /**
   * 上报屏幕录制事件
   */
  static reportScreenRecord(action: 'start' | 'stop' | 'pause' | 'resume', duration?: number): void {
    reportEvent(`screen_record_${action}`);

    const logData: any = {
      action,
      timestamp: Date.now()
    };

    if (duration !== undefined) {
      logData.duration = duration;
    }

    info(`Screen record ${action}`, logData);

    // 如果是停止录制，上报录制时长
    if (action === 'stop' && duration) {
      reportTime('screen_record_duration', duration);
    }
  }

  /**
   * 上报错误信息
   */
  static reportError(errorType: string, errorMessage: string, stack?: string, extra?: Record<string, any>): void {
    error(`Error: ${errorType}`, {
      errorType,
      errorMessage,
      stack,
      timestamp: Date.now(),
      ...extra
    });
  }

  /**
   * 上报性能指标
   */
  static reportPerformance(metricName: string, value: number, unit: string = 'ms'): void {
    reportTime(metricName, value);

    info(`Performance metric: ${metricName}`, {
      metricName,
      value,
      unit,
      timestamp: Date.now()
    });
  }

  /**
   * 上报自定义业务事件
   */
  static reportCustomEvent(eventName: string, data: Record<string, any>): void {
    reportEvent(`custom_${eventName}`);

    report(`Custom event: ${eventName}`, {
      eventName,
      ...data,
      timestamp: Date.now()
    });
  }

  /**
   * 批量上报多个事件
   */
  static reportBatchEvents(events: Array<{ name: string; data?: Record<string, any> }>): void {
    events.forEach(event => {
      reportEvent(event.name);
      if (event.data) {
        info(`Batch event: ${event.name}`, {
          eventName: event.name,
          ...event.data,
          timestamp: Date.now()
        });
      }
    });
  }

  /**
   * 上报应用启动相关指标
   */
  static reportAppStartup(startTime: number, endTime: number): void {
    const duration = endTime - startTime;

    reportTime('app_startup_time', duration);
    reportEvent('app_startup_complete');

    info('App startup completed', {
      startTime,
      endTime,
      duration,
      timestamp: Date.now()
    });
  }

  /**
   * 上报网络请求相关
   */
  static reportNetworkRequest(url: string, method: string, status: number, duration: number): void {
    const eventName = status >= 200 && status < 300 ? 'network_request_success' : 'network_request_error';

    reportEvent(eventName);
    reportTime('network_request_duration', duration);

    const message = `Network request: ${method} ${url}`;
    const data = {
      url,
      method,
      status,
      duration,
      timestamp: Date.now()
    };

    if (status >= 200 && status < 300) {
      info(message, data);
    } else {
      error(message, data);
    }
  }
}

/**
 * 常用事件名称常量
 */
export const AEGIS_EVENTS = {
  // 用户相关
  USER_LOGIN: 'user_login',
  USER_LOGOUT: 'user_logout',
  
  // 页面相关
  PAGE_VIEW: 'page_view',
  PAGE_LEAVE: 'page_leave',
  
  // AI功能相关
  AI_CHAT_START: 'ai_chat_start',
  AI_CHAT_SEND: 'ai_chat_send',
  AI_CHAT_RECEIVE: 'ai_chat_receive',
  AI_CHAT_END: 'ai_chat_end',
  
  // 屏幕录制相关
  SCREEN_RECORD_START: 'screen_record_start',
  SCREEN_RECORD_STOP: 'screen_record_stop',
  SCREEN_RECORD_PAUSE: 'screen_record_pause',
  SCREEN_RECORD_RESUME: 'screen_record_resume',
  
  // 应用相关
  APP_STARTUP: 'app_startup_complete',
  APP_ERROR: 'app_error',
  
  // 自定义事件
  CUSTOM_EVENT: 'custom_event_metric'
} as const;
